/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.RRHH;

import gui.Almacen.*;
import Controlador.Bean.Almacen.EntradaSalida;
import Controlador.Bean.RRHH.Personal;
import Controlador.Service.RRHH.ServicioPersonal;
import gui.General.Main;
import java.awt.Component;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author César
 */
public class DetalleMantEfi extends javax.swing.JPanel {

    /**
     * Creates new form DetalleGestionAlmacen
     */
    String idAlmacen;
    TableModel tb;
    Date today = new Date();
    
    public DetalleMantEfi(String idAlmacen) {
        initComponents();
        this.idAlmacen = idAlmacen;
        initComp();
        tblProductos.setDefaultRenderer(JButton.class, new MiRender());
        tblProductos.setDefaultEditor( JButton.class, new MiEditor());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        pnProductos = new javax.swing.JScrollPane();
        tblProductos = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(700, 880));
        setMinimumSize(new java.awt.Dimension(0, 0));
        setPreferredSize(new java.awt.Dimension(784, 450));

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel1.setLayout(new java.awt.BorderLayout());

        tblProductos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblProductos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProductosMouseClicked(evt);
            }
        });
        pnProductos.setViewportView(tblProductos);

        jPanel1.add(pnProductos, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 660, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

   
    public class MiRender implements TableCellRenderer {
        // Este metodo indica como debe de pintarse el elemento en la fila row, en la columna column
        // que esta en la tabla table y tiene el color dado por el objeto.
        @Override
        public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {    
            // como el boton y el calendario son componentes esto se vale.
            return (JComponent)table.getValueAt(row, column);
        }
     }
     
     public class MiEditor extends AbstractCellEditor implements TableCellEditor {
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }
     }
    

    
    class TableModel extends AbstractTableModel{
              
        //Two arrays used for the table data
        String[] columnNames = {"Nombre", "Produccion", "Merma"
                        , "Fase"};
               
        List<EntradaSalida> data = Controlador.Service.Almacen.GestionAlmacenes.obtenerTransaccionesPorAlmacen(new Date(today.getTime() - 1 * 24 * 3600 * 1000 ), today, idAlmacen);
                
        
        @Override
        public int getRowCount()
        {
            return data.size();
        }
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {        
//            switch(column){
//                case 0: return data.get(row).getIdEntradaSalida();
//                case 1: return data.get(row).getTipo();
//                case 2: return data.get(row).getDescripcion();
//                case 3: return data.get(row).getFecha();
//                default: return new JButton("Detalle");
//            }
            return "0";
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
           return false;
        }
        
        
    } 
    
    private void initComp(){
        tb = new TableModel();
        tblProductos.setModel(tb);
//        Calendar calendarA = dpFechaFin.getMonthView().getCalendar();
//        // starting today if we are in a hurry
//        calendarA.setTime(new Date());
//        dpFechaInicio.getMonthView().setUpperBound(calendarA.getTime());
//        dpFechaFin.getMonthView().setUpperBound(calendarA.getTime());
//        dpFechaFin.setDate(today);
//        dpFechaFin.getMonthView().setLowerBound(calendarA.getTime());
//        dpFechaInicio.setDate(new Date(today.getTime() - 1 * 24 * 3600 * 1000 ));
//        List<Personal> personal =  ServicioPersonal.SeleccionarPersonal();
//        cmbEncargado.removeAllItems();
//        cmbAutorizado.removeAllItems();
//        cmbEncargado.addItem("Seleccionar");
//        cmbAutorizado.addItem("Seleccionar");
//        String data;
//        for(int i=0; i<personal.size();i++){
//            data = personal.get(i).getNombre()+" "+personal.get(i).getApellidoPaterno();
//            cmbEncargado.addItem(data);
//            cmbAutorizado.addItem(data);
//        }
    }
    
    private void tblProductosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProductosMouseClicked
        // TODO add your handling code here:
        int selectedRowIndex = tblProductos.getSelectedRow();
        int selectedColumnIndex = tblProductos.getSelectedColumn();
        DetalleTransaccion dt = new DetalleTransaccion(tb.data.get(selectedRowIndex));
        Main.getDesktop().add(dt);
        dt.setVisible(true);
    }//GEN-LAST:event_tblProductosMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane pnProductos;
    private javax.swing.JTable tblProductos;
    // End of variables declaration//GEN-END:variables
}
